package com.spzx.order.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.spzx.domain.OrderInfo;
import com.spzx.domain.OrderItem;
import com.spzx.order.mapper.OrderInfoMapper;
import com.spzx.order.mapper.OrderItemMapper;
import com.spzx.order.mapper.OrderLogMapper;
import com.spzx.order.service.IOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单Service业务层处理
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    OrderLogMapper orderLogMapper;


    /**
     * 查询用户订单列表
     *
     * @param orderStatus 订单状态
     * @return
     */
    @Override
    public List<OrderInfo> selectUserOrderInfoList(Integer orderStatus) {
        // 获取用户id
        Long userId = 1L;
        // 查询订单列表
        List<OrderInfo> orderInfoList = baseMapper.selectList(
                new LambdaQueryWrapper<OrderInfo>()
                        .eq(OrderInfo::getUserId, userId)
                        // 这里是条件查询,因为查询全部订单时,orderStatus为空
                        .eq(orderStatus != null, OrderInfo::getOrderStatus, orderStatus)
                        // 根据创建时间倒序排列
                        .orderByDesc(OrderInfo::getCreateTime)
        );
        if (!CollectionUtils.isEmpty(orderInfoList)) {
            List<Long> orderIdList = orderInfoList.stream().map(OrderInfo::getId).collect(Collectors.toList());

            //查询orderItem
            List<OrderItem> orderItemList = orderItemMapper.selectList(
                    new LambdaQueryWrapper<OrderItem>().in(OrderItem::getOrderId, orderIdList)
            );
            // groupBy:分组函数,因为order_info表和order_item表是一对多关系,需要分组
            Map<Long, List<OrderItem>> orderIdToOrderItemListMap = orderItemList.stream().collect(
                    Collectors.groupingBy(OrderItem::getOrderId)
            );

            //组装orderItemList
            orderInfoList.forEach(item -> {
                item.setOrderItemList(orderIdToOrderItemListMap.get(item.getId()));
            });
        }
        return orderInfoList;
    }

    @Override
    public OrderInfo getByOrderNo(String orderNo) {
        OrderInfo orderInfo = baseMapper.selectOne(
                new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getOrderNo, orderNo)
        );
        List<OrderItem> orderItemList = orderItemMapper.selectList(
                new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getOrderId, orderInfo.getId())
        );
        orderInfo.setOrderItemList(orderItemList);
        return orderInfo;
    }
}
